package org.sippr.rvt2ifc.service.impl;

import org.sippr.rvt2ifc.dao.ProjectDao;
import org.sippr.rvt2ifc.dao.ProjectModelRelationDao;
import org.sippr.rvt2ifc.dao.UploadedFileDao;
import org.sippr.rvt2ifc.dao.XktModelDao;
import org.sippr.rvt2ifc.pojo.*;
import org.sippr.rvt2ifc.service.ProjectModelRelationService;
import org.sippr.rvt2ifc.service.ProjectService;
import org.sippr.rvt2ifc.service.UploadedFileService;
import org.sippr.rvt2ifc.service.XktModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.sippr.rvt2ifc.utils.SnowflakeIdGenerator;

import java.util.*;

@Service
public class UploadedFileImpl implements UploadedFileService {
    @Autowired
    private UploadedFileDao uploadedFileDao;
    @Autowired
    private XktModelDao xktModelDao;
    @Autowired
    private ProjectDao projectDao;
    @Autowired
    private ProjectModelRelationDao projectModelRelationDao;
    @Autowired
    private ProjectService projectService;

    @Override
    public Project insert(UploadedFile uploadedFile) {
        uploadedFile.setUuid(SnowflakeIdGenerator.nextId());
        uploadedFileDao.insert(uploadedFile);

        if (!uploadedFile.getFileName().endsWith(".rvt")) {
            return null;
        }

        XktModel xktModel = new XktModel();
        xktModel.setUuid(SnowflakeIdGenerator.nextId());
        xktModel.setUploadedFileUuid(uploadedFile.getUuid());
        xktModel.setState((short) 0);
        xktModelDao.insert(xktModel);

        Project project = new Project();
        project.setUuid(SnowflakeIdGenerator.nextId());
        project.setProjectName(uploadedFile.getFileName());
        project.setState((short) 3);
        project.setDescription("");
        projectDao.insert(project);

        ProjectModelRelation projectModelRelation = new ProjectModelRelation();
        projectModelRelation.setProjectUuid(project.getUuid());
        projectModelRelation.setXktModelUuid(xktModel.getUuid());
        projectModelRelationDao.insert(projectModelRelation);
        return projectService.selectByUuid(project.getUuid());

    }

    /*
     * 获取等待处理的文件
     *
     * @param null
     * @return
     * @author xuegang
     * @create 2025/1/24
     **/

    @Override
    public List<UploadedFile> getWaitFile() {
        List<XktModel> xkts = xktModelDao.selectByState(0);
        List<UploadedFile> list = new ArrayList<>();
        for (XktModel xkt : xkts) {
            UploadedFile uploadedFile = uploadedFileDao.selectByUuid(xkt.getUploadedFileUuid());
            uploadedFile.setModelUuid(xkt.getUuid());
            Project project = getLonelyProject(xkt.getUuid());
            if (project == null) {
                continue;
            }
            uploadedFile.setProjectUuid(project.getUuid());
            list.add(uploadedFile);
        }
        return list;
    }

    private Project getLonelyProject(String modelUuid) {
        List<ProjectModelRelation> projectModelRelationList = projectModelRelationDao.selectByModelUuid(modelUuid);
        for (ProjectModelRelation projectModelRelation : projectModelRelationList) {
            Project project = projectDao.selectByUuidAndState(projectModelRelation.getProjectUuid(), 3);
            if (project != null)
                return project;
        }
        return null;
    }
}
